<template>
  <view>
    <nav-bar title="康复辅具"></nav-bar>
    <you-scroll
      :loadingHeight="loadingHeight"
      :mainHeight="scrollHeight"
      :isAllData="isAllData"
      :isRequestSuccess="isRequestSuccess"
      @onRefresh="onRefresh"
      @loadMore="loadMore"
    >
      <template>
        <view v-if="isLoading" class="common-loading">
          <image class="loading-img" :src="baseIconUrl + '/commonIcon/icon_loading.png'" />
          正在加载数据，请稍后......
        </view>
        <view class="common-no-data" v-else-if="!orthosesList || orthosesList.length === 0">
          <image class="no-data-img" :src="baseIconUrl + '/commonIcon/icon_no_data.png'" />
          <view class="text">暂无数据</view>
        </view>
        <view v-else class="orthoses-list">
          <view
            class="orthoses-item"
            v-for="(item, index) in orthosesList"
            :key="index"
            @click="clickOrthosesItem(item)"
          >
            <view class="img-box">
              <image
                class="img"
                :src="item.deviceImg || baseImgUrl + '/userImg/img_service_small.png'"
                mode="aspectFill"
                />
            </view>
            <view class="content-box">
              <view class="name">{{ item.deviceName }}</view>
              <view class="btn">立即购买</view>
            </view>
          </view>
        </view>
      </template>
    </you-scroll>
  </view>
</template>

<script>
  import api_orthoses from '@/api/user.js'
  import { baseUrl, baseImgUrl, baseIconUrl } from '@/config'
  export default {
    name: 'OrthosesList',
    data() {
      return {
        baseUrl,
        baseImgUrl,
        baseIconUrl,
        loadingHeight: '',
        scrollHeight: '',
        isAllData: false,
        isRequestSuccess: false,
        isLoading: false,
        orthosesList: [],
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
    },
    onLoad() {
      this.scrollHeight = this.$store.getters.heightData.noTabMainHeight + 'px'
      this.loadingHeight = this.$store.getters.heightData.noTabMainHeight - 20 + 'px'
      this.pageNum = 1
      this.orthosesList = []
      this.isLoading = true
      this.getOrthosesList()
    },
    methods: {
      getOrthosesList() {
        const params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        api_orthoses.deviceList(params).then(res => {
          const result = res.rows
          result.forEach(item =>{
            if (!item.deviceImg) return
            item.deviceImg = item.deviceImg.includes('http') ? item.deviceImg : this.baseUrl + item.deviceImg            
          })
          this.orthosesList.push(...result)
          this.total = res.total
          this.isRequestSuccess = true
          this.isLoading = false
      })
      },
      clickOrthosesItem(item) {
        uni.navigateTo({
          url: `/subPages/userIndex/rehabilitationAids/orthosesDetail?id=${item.id}`
        })
      },
      // 下拉刷新
      onRefresh() {
        this.pageNum = 1
        this.orthosesList = []
        this.isAllData = false
        this.isRequestSuccess = false
        this.getOrthosesList()
      },
      // 上拉加载
      loadMore() {
        if (this.orthosesList.length < this.total) {
          this.pageNum++
          this.isAllData = false
          this.isRequestSuccess = false
          this.getOrthosesList()
        } else {
          this.isAllData = true
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .orthoses-list {
    .orthoses-item {
      display: flex;
      margin: 30rpx;
      padding: 24rpx 30rpx;
      border-radius: 16rpx;
      background-color: #FFF;
      .img-box {
        width: 288rpx;
        height: 192rpx;
        margin-right: 20rpx;
        .img {
          width: 100%;
          height: 100%;
        }
      }
      .content-box {
        position: relative;
        flex: 1;
        .name {
          margin-top: 16rpx;
          line-height: 44rpx;
          font-size: 32rpx;
          font-weight: 500;
          color: #000;
        }
        .btn {
          position: absolute;
          right: 0;
          bottom: 0;
          width: 130rpx;
          height: 48rpx;
          border-radius: 24rpx;
          text-align: center;
          line-height: 48rpx;
          font-size: 24rpx;
          color: #FFF;
          background-color: #115BAA;
        }
      }
    }
  }
</style>
